<?php
if(!defined('EMLOG_ROOT')) {exit('error!');}
function plugin_setting_view(){
        ?>
        <style>
        .card_setting label{
                padding-bottom:10px;
                display:block;
        }
        </style>
        <form class="card_setting" method="post">
        <input type="text" hidden="hidden" name="post_id" value="<?php echo rand(1, 999999); ?>" /> 
        <input type="hidden" name="settingType" value="setting">
        <label>生成数量：<input type="text" autocomplete="off" name="count" value="0" /></label>
        <label>序列号类型（数字）：<input type="text" autocomplete="off" name="type" value="0" /></label>
        <label><span style="vertical-align: top;">附加数据：</span><textarea name="exData" style="width:500px;height:200px" placeholder="附加数据" value=""></textarea></label>
        <br />

        <input type="submit" value="提交" />

        </form>
        <?php
	
}
function unique_rand($min, $max, $num) {
        $count = 0;
        $return = array();
        while ($count < $num) {
                $return[] = mt_rand($min, $max);
                $return = array_flip(array_flip($return));
                $count = count($return);
        }
        shuffle($return);
        return $return;
}
if(!empty($_POST)){
        if($_POST['settingType']=='setting'){
                $addCount = intval($_POST['count']);
                if(!empty($addCount)){
                        $DB = MySql::getInstance();
                        $serialNumberArr = array();
                        $randomArr = unique_rand(1,99999999,$addCount);
                        $randomAlpha = array('a','b','c','d','e','f');
                        for($i=0; $i<$addCount; $i++){
                                $timeNumber = microtime(true)*10000;
                                $timeNumber = strval(dechex($timeNumber));
                                $randomNumber = sprintf("%08d",$randomArr[$i]);
                                $serialNumber = $randomAlpha[mt_rand(0, 5)].$timeNumber.$randomAlpha[mt_rand(0, 5)].$randomNumber.$randomAlpha[mt_rand(0, 5)].$randomAlpha[mt_rand(0, 5)].$randomAlpha[mt_rand(0, 5)];
                                array_push($serialNumberArr,$serialNumber);
                        }
                        for($j=0; $j<count($serialNumberArr); $j++){
                                $sqli="INSERT INTO ".DB_PREFIX."wm_serial_number (serialNumber,state,useTime,type,exData) VALUES('".$serialNumberArr[$j]."',0,0,0,'".base64_encode($_POST['exData'])."')";
                                $DB->query($sqli);
                        }
                        
                }
        }
        header("Location: {$_SERVER['REQUEST_URI']}");
}

?>